﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Text;
using System.Text.RegularExpressions;
using MvcFckEditor.Components;
using MvcFckEditor.Models;
using System.Drawing;

namespace MvcFckEditor.Controllers
{
    public class FckEditorController : Controller
    {
        public string Upload()
        {
            string strValue = string.Empty;
            string sResourceType = Request.QueryString["Type"];

            Config objConfig = new Config();
            strValue = objConfig.FileUpload(Request.Files["NewFile"], sResourceType, "/", true);
            return strValue;
        }

        public ActionResult UploadFile(string fFolder, string ftype)
        {
            Config objConfig = new Config();
            this.TempData["Error"] = objConfig.FileUpload(Request.Files["NewFile"], ftype, fFolder, false);
            return RedirectToAction("Browser", new { currentFolder = fFolder, type = ftype }); ;
        }

        public ActionResult Browser(string currentFolder, string type)
        {
            currentFolder = (currentFolder ?? "").EndsWith("/") ? (currentFolder ?? "") : ((currentFolder ?? "") + "/");
            
            this.ViewData["CurrentFolder"] = currentFolder;
            this.ViewData["Type"] = type;
            switch(type)
            {
                case "Image":
                    this.ViewData["PageTitle"] = "选择图片";
                    break;
                case "Flash":
                    this.ViewData["PageTitle"] = "选择Flash";
                    break;
                default:
                    this.ViewData["PageTitle"] = "选择文件";
                    break;
            }
            //获取当前的文件和文件夹
            Config objConfig = new Config();
            string[] arrDir = Directory.GetDirectories(HttpContext.Server.MapPath(objConfig.UserFilesPath + currentFolder));
            string[] arrFile = Directory.GetFiles(HttpContext.Server.MapPath(objConfig.UserFilesPath + currentFolder));
            List<FckItemInfo> list = new List<FckItemInfo>();

            if (currentFolder != "/")
            {
                list.Add(new FckItemInfo { Icon = objConfig.FckBasePath + "Up.jpg", Height = 89, Width = 100, Name = "上层目录", Tip = "上层目录", Url = "/FckEditor/browser?type=" + type + "&currentFolder=" + currentFolder.Substring(0, currentFolder.Substring(0, currentFolder.Length - 1).LastIndexOf('/') + 1) });
            }
            foreach (string strDir in arrDir)
            {
                DirectoryInfo objDir = new DirectoryInfo(strDir);
                list.Add(new FckItemInfo { Icon = objConfig.FckBasePath + "Folder.jpg", Height = 100, Width = 94, Name = objDir.Name, Tip = objDir.Name, Url = "/FckEditor/browser?type=" + type + "&currentFolder=" + currentFolder + objDir.Name });
            }
            foreach (string strFile in arrFile)
            {
                FileInfo objFile = new FileInfo(strFile);
                string strE = objFile.Extension.ToLower().Substring(1);
                //只有合乎当前类型的文件才允许被选中
                if (objConfig.TypeConfig[type].AllowedExtensions.Contains(strE))
                {
                    try
                    {
                        Image objImage = Image.FromFile(strFile);
                        list.Add(new FckItemInfo { Icon = objConfig.UserFilesPath + currentFolder + objFile.Name, Width = objImage.Width, Height = objImage.Height, Name = objFile.Name, Url = "javascript:OpenFile('" + objConfig.UserFilesPath + currentFolder + objFile.Name + "');" });
                    }
                    catch
                    {
                        string strIcon = objConfig.FckBasePath + "filethumbnails/" + strE + ".gif";
                        if (!System.IO.File.Exists(HttpContext.Server.MapPath(strIcon)))
                        {
                            strIcon = objConfig.FckBasePath + "filethumbnails/unknown.gif";
                        }
                        list.Add(new FckItemInfo { Icon = strIcon, Height = 32, Width = 32, Name = objFile.Name, Tip = objFile.Name, Url = "javascript:OpenFile('" + objConfig.UserFilesPath + currentFolder + objFile.Name + "');" });
                    }
                }
            }
            return this.View(list);
        }

        public ActionResult CreateFolder(string cFolder, string newFolder, string ctype)
        {
            string newF = Util.SanitizeFolderName(newFolder);
            if (!String.IsNullOrEmpty(newF))
            {
                cFolder = cFolder + Util.SanitizeFolderName(newFolder);
                Config objConfig = new Config();
                Util.CreateDirectory(HttpContext.Server.MapPath(objConfig.UserFilesPath + cFolder));
            }
            return RedirectToAction("Browser", new { currentFolder = cFolder, type = ctype });
        }
    }
}
